package com.liy.cloud.common.trade.entity;

import com.liy.cloud.common.exception.NegativeNumberException;
import com.liy.cloud.common.trade.utils.TradeMessageParser;

import java.io.Serializable;
import java.text.ParseException;

/**
 * 售卡交易数据明细表实体类 作者: 日期：2018-08-12 17:12:59 描述：
 */
public class TradeSell extends TradeComm implements Serializable {

	private static final long serialVersionUID = 1L;

	/*
	 * 主表ID
	 */
	private String commId;

	/*
	 * 售票目的站点
	 */
	private String destinationSite;

	/*
	 * 有效起始时间
	 */
	private String startTime;

	/*
	 * 有效终止时间
	 */
	private String endTime;

	/*
	 * 前次交易终端代号
	 */
	private String exDeviceId;

	/*
	 * 前次交易类型
	 */
	private String exTradeCode;

	/*
	 * 前次交易时间
	 */
	private String exTradeTime;

	/*
	 * 前次交易金额
	 */
	private Long exTradeMoney;

	/*
	 * 前次交易操作员号
	 */
	private String exOper;

	/**
	 * 默认构造方法
	 **/
	public TradeSell() {
		super();
	}

	/**
	 * getger和setter方法
	 */

	public String getCommId() {
		return commId;
	}

	public void setCommId(String commId) {
		this.commId = commId;
	}

	public String getDestinationSite() {
		return destinationSite;
	}

	public void setDestinationSite(String destinationSite) {
		this.destinationSite = destinationSite;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) throws ParseException {
		checkTimeStyle(startTime, "StartTime");
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) throws ParseException {
		checkTimeStyle(endTime, "EndTime");
		this.endTime = endTime;
	}

	public String getExDeviceId() {
		return exDeviceId;
	}

	public void setExDeviceId(String exDeviceId) {
		this.exDeviceId = exDeviceId;
	}

	public String getExTradeCode() {
		return exTradeCode;
	}

	public void setExTradeCode(String exTradeCode) {
		this.exTradeCode = exTradeCode;
	}

	public String getExTradeTime() {
		return exTradeTime;
	}

	public void setExTradeTime(String exTradeTime) throws ParseException {
		checkTimeStyle(exTradeTime, "ExTradeTime");
		this.exTradeTime = exTradeTime;
	}

	public Long getExTradeMoney() {
		return exTradeMoney;
	}

	public void setExTradeMoney(Long exTradeMoney) throws NegativeNumberException {
		checkMoneyStyle(exTradeMoney, "ExTradeMoney");
		this.exTradeMoney = exTradeMoney;
	}

	public String getExOper() {
		return exOper;
	}

	public void setExOper(String exOper) {
		this.exOper = exOper;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	/**
	 * 根据版本容解析交易内容
	 * 
	 * @param message
	 * @throws Exception
	 */
	public TradeSell init(String message) throws Exception {

		TradeMessageParser messageParser = null;
		if(message.startsWith("SZMETROV3")) {

            /** 根据报文格式长度来定义， -1为行业分割符
             * 一、V3卡号由9位变为20位
             * 二、增加字段：
             付费方式
             支付方式
             发卡方机构码
             发卡方交易流水号
             运营模式
             三、票卡发行方标识	从长度4变为长度2
             */
			int[] splitNumber = { 9, 8, 20, 2, 10,   9, 6, 14, 10, 8,   8, 8, 8, 5, 10,   14, 14, 10, 9, 2,
                    14, 8, 6, 20, 10,   2, 2, 16, 1, 1,   2, 2, 2, 4, 4,
					1, 2, 8, 10, 4, //V3新增
					1, 2, 5, -1, 5 };

			messageParser = new TradeMessageParser(message, splitNumber);
			this.setVersion(messageParser.getFiled(0));
			this.setTac(messageParser.getFiled(1));
			this.setCardId(messageParser.getFiled(2));

			this.setTradeCode(messageParser.getFiled(3));
			this.setTradeAddress(messageParser.getFiled(4));
			this.setDeviceId(messageParser.getFiled(5));
			this.setOperator(messageParser.getFiled(6));
			this.setTradeTime(messageParser.getFiled(7));
			this.setDeviceSeq(Long.parseLong(messageParser.getFiled(8)));
			this.setTradeMoney(Long.parseLong(messageParser.getFiled(9)));
			this.setTradeValue(Long.parseLong(messageParser.getFiled(10)));
			this.setDeposit(Long.parseLong(messageParser.getFiled(11)));
			this.setCost(Long.parseLong(messageParser.getFiled(12)));
			this.setCardSeq(Long.parseLong(messageParser.getFiled(13)));
			this.setDeviceSum(Long.parseLong(messageParser.getFiled(14)));
			this.setStartTime(messageParser.getFiled(15));
			this.setEndTime(messageParser.getFiled(16));
			this.setDestinationSite(messageParser.getFiled(17));
			this.setExDeviceId(messageParser.getFiled(18));
			this.setExTradeCode(messageParser.getFiled(19));
			this.setExTradeTime(messageParser.getFiled(20));
			this.setExTradeMoney(Long.parseLong(messageParser.getFiled(21)));
			this.setExOper(messageParser.getFiled(22));
			this.setSamcardId(messageParser.getFiled(23));
			this.setSamcardSeq(Long.parseLong(messageParser.getFiled(24)));
			this.setCardCode(messageParser.getFiled(25));
			this.setTradeSub(messageParser.getFiled(26));
			this.setPhysicalId(messageParser.getFiled(27));
			this.setTestFlag(messageParser.getFiled(28));
			this.setGreyFlag(messageParser.getFiled(29));
			this.setMedia(messageParser.getFiled(30));
			this.setCardVersion(messageParser.getFiled(31));
			this.setSubCardCode(messageParser.getFiled(32));
			this.setCityCode(messageParser.getFiled(33));
			this.setBusinessCode(messageParser.getFiled(34));

			// V3增加字段
			this.setPayMethod(messageParser.getFiled(35));
			this.setPayType(messageParser.getFiled(36));
			this.setIssuerCode(messageParser.getFiled(37));
			this.setIssuerSeq(messageParser.getFiled(38));
			this.setOperMode(messageParser.getFiled(39));

			this.setIndustrySplit(messageParser.getFiled(40));
			this.setIssuer(messageParser.getFiled(41));
			super.setIndustryDataLength(Long.parseLong(messageParser.getFiled(42)));
			super.setIndustryData(messageParser.getFiled(43));
			super.setCrc16(messageParser.getFiled(44));
		}
		return this;
	}

	@Override
	public String toString() {
		return super.toString() + "\n{\"TradeSell\": {\"commId\": \"" + commId + "\", \"destinationSite\": \""
				+ destinationSite + "\", \"startTime\": \"" + startTime + "\", \"endTime\": \"" + endTime
				+ "\", \"exDeviceId\": \"" + exDeviceId + "\", \"exTradeCode\": \"" + exTradeCode
				+ "\", \"exTradeTime\": \"" + exTradeTime + "\", \"exTradeMoney\": \"" + exTradeMoney
				+ "\", \"exOper\": \"" + exOper + "\"}}";
	}

}